<?php
require './include/global.php';
require './include/validate.php';
require './include/all_left.php';

require_once './include/advisory_db.php';
$status = $_SESSION['status'];
$isTr = 'Trial' != $status && 'Standard' != $status;
$smarty->assign('isTr',$isTr);
//print_r($_SESSION);
$smarty->compile_check = true;


$smarty->display('header.tpl');
if($_GET[country]==''){
//$_GET[country] = 211;
}
$smarty->assign('s',$_GET);
//jobupdate
$smarty->assign('jobupdate_names',$UPDATE);
$smarty->assign('state',$state);
$smarty->assign('update',$UPDATE);
$smarty->assign('country',$country);
$smarty->assign('industry',$jobcategory);
$smarty->assign('function',$jobfunction);
$smarty->assign('salary',$salary);
$smarty->assign('jobtype',$jobtype);
//$smarty->assign('rec_findresume',$rec_findresume);	

//add searchbox by user type
$usertype_result = $DB->query("SELECT re_usertype FROM pa_resume WHERE re_usertype != '' GROUP BY re_usertype");

$i=1;
$rec_usertype[0] = "-- SELECT ALL --";
while($row = $DB->fetch_array($usertype_result)) {
	$rec_usertype[$row[re_usertype]] = ucwords($row[re_usertype]);
}
$smarty->assign('usertype',$rec_usertype);

//can only search for the resume submitted by friends
if($_SESSION['status'] <> 'Premium'){
	$fidArr = friendList($_SESSION['uid']);
	if(!empty($fidArr)){
		$inIds = @implode(",",$fidArr);
		$extsql =  " AND (( re_usertype = 'recruiter' AND re_uid IN($inIds)) OR re_usertype <> 'recruiter') ";
	}else{
		$extsql =  " AND re_usertype <> 'recruiter' ";
	}
}

$where = "  WHERE 1=1 AND  `re_private` <> 1  $extsql";
if($_GET[action]!='search') $where .= " AND re_aliasid = '0'  ";
$order = " ORDER BY re_id DESC";

if($_GET[action]=="search"){ 
	$keywords = mysql_real_escape_string($_GET['keywords']);

	if( $keywords!=''){ 
		$myVars = split(" ",$keywords);
		$keywords_str = array();
		foreach($myVars as $search) {
			$keywords_str[] = " CONCAT_WS(' ', `re_detail`, `re_crtjobtitle`, `re_jobtitle`) LIKE '%".$search."%' ";
		}
		$new_keywords_str = @implode("OR", $keywords_str);
		if($new_keywords_str) {
			$arr[] = "(".$new_keywords_str.")";
		}
	}	
	/*
	if($_GET[orderby]=='0'){$by = "re_id";}
	else{
		$ord   = array('','re_locity','re_lostate','re_locountry','re_jobindustry','re_jobtitle','re_jobtype','re_proflicense','re_regtime');
		$by = $ord[$_GET[orderby]];
	}*/
    if(empty($by)){ $by = "re_id";}
	if($_GET[city]!='')      { $arr[] = " `re_locity` like '%".$_GET[city]."%' ";}
    if($_GET[state]!='0')    { $arr[] = " `re_lostate` = '".$_GET[state]."'  ";}
	if($_GET[country]!='0')  { $arr[] = " `re_locountry` = '".$_GET[country]."'  ";}
	if($_GET[industry]!='0') { $arr[] = " `re_jobindustry` = '".$_GET[industry]."'  ";}
	//if($_GET[jobtitle]!='')  { $arr[] = " `re_jobtitle`  like '%".$_GET[jobtitle]."%'  ";}	
	//if($_GET[jobtype]!='0' ) { $arr[] = " `re_jobtype`  =  '".$_GET[jobtype]."'  ";}
	if($_GET[certif]!='' )   { $arr[] = " `re_proflicense` like '%".$_GET[certif]."%'  ";}
    if($_GET[jobupdate]!='') { $arr[] = " (UNIX_TIMESTAMP( ) - `re_regtime`) < (3600*24*".$_GET[jobupdate].")" ;}
	if($_GET[usertype]!='0') { $arr[] = " `re_usertype` = '".$_GET[usertype]."' " ;}
	if($_GET[alias]!=1)      { $arr[] = "  re_aliasid = '0'  "; }

	//print_r($arr);
	if(!empty($arr)) {
		$implode  = @implode("AND", $arr);
		$implode  = " AND ".$implode;
		$where    .= $implode;
	}
    $order = " ORDER BY $by DESC ";
}

//echo "SELECT * FROM pa_resume $where $order";
$nav = new buildNav;
$nav->limit = 25;
$total = $DB->fetch_one_array("SELECT COUNT(*) AS count FROM pa_resume $where");
$nav->total_result = $total[count];
$nav->execute("SELECT * FROM pa_resume $where $order");

$page = $nav->pagenav();
//echo $sql2;

$i=0;
while($list2 = $DB->fetch_array($nav->sql_result)){
   $viewed = $DB->fetch_one_array("SELECT b.accessid FROM pa_userrecords AS a LEFT JOIN pa_userrecords_data AS b ON a.id = b.urcid WHERE a.userid = '".$_SESSION[uid]."' AND a.usertype='recruiter' AND a.type='0' AND a.date = '".date('Y-m')."' AND b.accessid = '".$list2[re_id]."'");

	$datajob[$i]              = $list2;
	$datajob[$i][name]        = ($list2[re_usertype] == 'recruiter')?$list2[re_nickname] : $list2[re_fname]." ".$list2[re_lname] ;
	$datajob[$i][industry]    = ($list2[re_jobindustry]>0)?$jobcategory[$list2[re_jobindustry]]:"";
	if($list2[re_locity])		{$datajob[$i][location] .= $list2[re_locity]."," ;}
	if($list2[re_lostate]>0)	{$datajob[$i][location] .= $state[$list2[re_lostate]]."," ;}
	if($list2[re_locountry]>0)	{$datajob[$i][location] .= $country[$list2[re_locountry]] ;}
	$datajob[$i][re_jobtype]  = $jobtype[$list2[re_jobtype]];
	$datajob[$i][re_style]    = $viewed[accessid] ? "blue" : 'gery';
	$datajob[$i][foto]		  = ($list2['re_upload']!='')?"<img src='./uploadfiles/resumes/".$list2['re_upload']."' width='100'>":"<img src='./images/df_avatar.jpg' width='100' />";
	$aliasnum                 = $DB->fetch_one_array("SELECT re_id FROM pa_resume WHERE re_fid = '".$datajob[$i][re_id]."'");

	if(!empty($aliasnum)){
	$datajob[$i][alias]    = 1;
	}
	$i++;
}

//user records for standard user
if($_SESSION['status'] == 'Standard' || $_SESSION['status'] == 'Trial'){
	$smarty->assign("refreshpage",1);
	$userrecords = $DB->fetch_one_array("SELECT amount FROM pa_userrecords WHERE userid = '".$_SESSION[uid]."' AND usertype='recruiter' AND type='0' AND date = '".date('Y-m')."'");
	
	if($_SESSION['status'] == 'Standard'){$sqlKey = 'recruiter_resume_view';}
	if($_SESSION['status'] == 'Trial'){$sqlKey = 'starter_resume_view';}

	$records_setting = $DB->fetch_one_array("SELECT `value` FROM pa_settings WHERE `key` = '".$sqlKey."' ");
	$views_left = $records_setting['value'] - $userrecords['amount'];
	$views_left = ($views_left>=0)?$views_left:0;
	$smarty->assign("views_left",$views_left);
}


$smarty->assign("resumelist",$datajob);
$smarty->assign("page",$page);
if($err!=''){
$smarty->assign("err",$err);
}
$smarty->assign("search",$reg_data);
$smarty->display('allleft.tpl');
$smarty->display('findresume.tpl');
$smarty->display('footer.tpl');
?>